﻿Ext.define("PSI.User.ChangeMyPasswordForm", {
    createUI: function (user) {
        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "修改我的登录密码",
            items: [
                {
                    region: "center", xtype: "panel", layout: "absolute", border: 0,
                    items: [
                        {
                            id: "editForm",
                            x: 200,
                            y: 50,
                            xtype: "form",
                            layout: "form",
                            height: 170,
                            width: 300,
                            defaultType: 'textfield',
                            border: 0,
                            fieldDefaults: {
                                labelWidth: 60,
                                labelAlign: "right",
                                labelSeparator: "",
                                msgTarget: 'side'
                            },
                            items: [
                                {
                                    xtype: "hidden",
                                    name: "id",
                                    value: user.id
                                },
                                {
                                    fieldLabel: "登录名",
                                    xtype: "displayfield",
                                    value: user.loginName
                                },
                                {
                                    fieldLabel: "姓名",
                                    xtype: "displayfield",
                                    value: user.name
                                },
                                {
                                    id: "editOldPassword",
                                    fieldLabel: "旧密码",
                                    allowBlank: false,
                                    blankText: "没有输入旧密码",
                                    beforeLabelTextTpl: PSI.Const.REQUIRED,
                                    inputType: "password",
                                    name: "oldPassword"
                                },
                                {
                                    id: "editNewPassword",
                                    fieldLabel: "新密码",
                                    allowBlank: false,
                                    blankText: "没有输入新密码",
                                    beforeLabelTextTpl: PSI.Const.REQUIRED,
                                    inputType: "password",
                                    name: "newPassword"
                                },
                                {
                                    id: "editConfirmPassword",
                                    fieldLabel: "确认密码",
                                    allowBlank: false,
                                    blankText: "没有输入确认密码",
                                    beforeLabelTextTpl: PSI.Const.REQUIRED,
                                    inputType: "password"
                                }
                            ],
                            buttons: [
                                { text: "修改密码", formBind: true, handler: this.onOK, scope: this, iconCls: "psi-button-ok" },
                                {
                                    text: "取消", handler: function () {
                                        location.replace("/");
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        });

        this.panel = panel;
    },

    getMainPanel: function () {
        return this.panel;
    },

    onOK: function () {
        var editNewPassword = Ext.getCmp("editNewPassword");
        var editConfirmPassword = Ext.getCmp("editConfirmPassword");

        var np = editNewPassword.getValue();
        var cp = editConfirmPassword.getValue();

        if (np != cp) {
            PSI.MsgBox.showInfo("确认密码与新密码不一致", function () {
                editNewPassword.focus();
            });
            return;
        }

        var form = Ext.getCmp("editForm");
        var el = Ext.getBody();
        form.submit({
            url: "/User/ChangeMyPasswordPOST",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo("成功修改登录密码", function () {
                    location.replace("/");
                });
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg);
            }
        });
    }
});